package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicDeviceDTO;
import com.iwomy.secureplat.platform.dto.BasicInventoryListDTO;
import com.iwomy.secureplat.platform.entity.BasicInventoryListEntity;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.BasicExpensesDeptDetailService;
import com.iwomy.secureplat.platform.service.BasicInventoryListService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出库列表-出库记录
 *
 * @author pig
 * @date 2023-12-12 11:13:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicInventoryList" )
@Tag(description = "basicInventoryList" , name = "出库列表-出库记录" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicInventoryListController {

    private final BasicInventoryListService basicInventoryListService;

	private final BasicDeviceService basicDeviceService;

	private final BasicExpensesDeptDetailService basicExpensesDeptDetailService;


	/**
     * 分页查询
     * @param page 分页对象
     * @param basicInventoryListDTO 出库列表-出库记录
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_view')" )
    public R getBasicInventoryListPage(@ParameterObject Page page, @ParameterObject BasicInventoryListDTO basicInventoryListDTO) {
        return R.ok(basicInventoryListService.getBasicInventoryListPage(page, basicInventoryListDTO));
    }

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param basicDeviceDTO 库存列表
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/inventoryList" )
	@PreAuthorize("@pms.hasPermission('platform_basicInventoryList_view')" )
	public R getBasicListPage(@ParameterObject Page page, @ParameterObject BasicDeviceDTO basicDeviceDTO) {
		return R.ok(basicDeviceService.pageList(page,basicDeviceDTO));
	}

    /**
     * 通过id查询出库列表-出库记录
     * @param outInventoryNumber
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{outInventoryNumber}" )
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_view')" )
    public R getById(@PathVariable("outInventoryNumber" ) String outInventoryNumber) {
        return R.ok(basicInventoryListService.getoutInventoryNumber(outInventoryNumber));
    }

    /**
     * 新增出库列表-出库记录
     * @param inventoryList 出库列表-出库记录
     * @return R
     */
    @Operation(summary = "新增出库列表-出库记录" , description = "新增出库列表-出库记录" )
    @SysLog("新增出库列表-出库记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_add')" )
    public R save(@RequestBody BasicInventoryListDTO inventoryList) {
		return basicInventoryListService.newlyAddedBatch(inventoryList);
    }

    /**
     * 修改出库列表-出库记录
     * @param basicInventoryList 出库列表-出库记录
     * @return R
     */
    @Operation(summary = "修改出库列表-出库记录" , description = "修改出库列表-出库记录" )
    @SysLog("修改出库列表-出库记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_edit')" )
    public R updateById(@RequestBody BasicInventoryListEntity basicInventoryList) {
        return R.ok(basicInventoryListService.updateById(basicInventoryList));
    }

    /**
     * 通过id删除出库列表-出库记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除出库列表-出库记录" , description = "通过id删除出库列表-出库记录" )
    @SysLog("通过id删除出库列表-出库记录" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicInventoryListService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicInventoryList 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicInventoryList_export')" )
    public List<BasicInventoryListEntity> export(BasicInventoryListEntity basicInventoryList,Long[] ids) {
        return basicInventoryListService.list(Wrappers.lambdaQuery(basicInventoryList).in(ArrayUtil.isNotEmpty(ids), BasicInventoryListEntity::getId, ids));
    }

	/**
	 * 查询企业可用年限
	 * @param  customerDeptId
	 * @return R
	 */
	@Operation(summary = "通过deptId查询" , description = "通过deptId查询" )
	@GetMapping("/getPlatformFeeYears/{customerDeptId}" )
	public R getPlatformFeeYears(@PathVariable("customerDeptId" ) Long customerDeptId) {
		return R.ok(basicExpensesDeptDetailService.getPlatformFeeYears(customerDeptId));
	}
}